<?php
require_once("root.php");

require_once($ROOT . "config.inc.php");

require_once("dataModelParser.php");
require_once("convert.php");

$dbhost = $database_host;
$dbuser = $database_user;
$dbpass = $database_pass;
$dbschema = $database_name;

$args = $_SERVER["argv"];

$i = 1;
$file = $args[$i];
while (isset($file))
{
  $p = &new dataModelParser();

  $result = $p->setInputFile($file);
  $result = $p->parse();
  $schema = $p->getSchema();
  $constraints = $p->getConstraints();
  $codeLookup = $p->getCodeLookupData();

  $sql = "drop database if exists $dbschema;\n";
  $sql .= "create database $dbschema;\n";
  $sql .= "use $dbschema;";
  $sql .= convertToSQL($schema, $constraints);
  $sql .= makeCodeLookupInserts($codeLookup);

  writeToFile("schema.sql", $sql);

  exec ("mysql --user=$dbuser --password=$dbpass < schema.sql");

  if ($addZipCodeData == true)
  {
  	exec ("mysql --user=$dbuser --password=$dbpass $dbschema < $zipcodes_file");
  }

  if (file_exists('testData.sql'))
  {
  	exec ("mysql --user=$dbuser --password=$dbpass $dbschema < testData.sql");
  }

  if ($generateDAO == true)
  {
	  convertToDAO($schema, $daoDir);

	  copy("BaseDAO.php", $ROOT . $daoDir . "BaseDAO.php");
  }
  $i++;
  if ($i < count($args))
    $file = $args["$i"];
  else
    unset($file);

}

?>
